package com.ruoyi.web.controller.driver;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.RolesEnums;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.driver.domain.DriverInfo;
import com.ruoyi.driver.service.IDriverInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 驾驶员信息Controller
 * 
 * @author ssh
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/driver/driverinfo")
public class DriverInfoController extends BaseController
{
    @Autowired
    private IDriverInfoService driverInfoService;


    @Autowired
    private ISysUserService userService;

    /**
     * 查询驾驶员信息列表
     */
    @PreAuthorize("@ss.hasPermi('driver:driverinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(DriverInfo driverInfo)
    {
        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        // 运输公司管理员
        if(RolesEnums.YUN_SHU_ADMIN.getRoleKey().equals(roleKey)){
            driverInfo.setTransportationCompanyUserId(user.getUserId());
        }
        startPage();
        List<DriverInfo> list = driverInfoService.selectDriverInfoList(driverInfo);
        return getDataTable(list);
    }

    /**
     * 导出驾驶员信息列表
     */
    @PreAuthorize("@ss.hasPermi('driver:driverinfo:export')")
    @Log(title = "驾驶员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DriverInfo driverInfo)
    {
        List<DriverInfo> list = driverInfoService.selectDriverInfoList(driverInfo);
        ExcelUtil<DriverInfo> util = new ExcelUtil<DriverInfo>(DriverInfo.class);
        util.exportExcel(response, list, "驾驶员信息数据");
    }

    /**
     * 获取驾驶员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('driver:driverinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(driverInfoService.selectDriverInfoById(id));
    }

    /**
     * 新增驾驶员信息
     */
    @PreAuthorize("@ss.hasPermi('driver:driverinfo:add')")
    @Log(title = "驾驶员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DriverInfo driverInfo)
    {
        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        // 运输公司管理员
        if(RolesEnums.YUN_SHU_ADMIN.getRoleKey().equals(roleKey)){
            driverInfo.setTransportationCompanyUserId(user.getUserId());
        }
        return toAjax(driverInfoService.insertDriverInfo(driverInfo));
    }

    /**
     * 修改驾驶员信息
     */
    @PreAuthorize("@ss.hasPermi('driver:driverinfo:edit')")
    @Log(title = "驾驶员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DriverInfo driverInfo)
    {
        return toAjax(driverInfoService.updateDriverInfo(driverInfo));
    }

    /**
     * 删除驾驶员信息
     */
    @PreAuthorize("@ss.hasPermi('driver:driverinfo:remove')")
    @Log(title = "驾驶员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(driverInfoService.deleteDriverInfoByIds(ids));
    }


    /**
     * 新增驾驶员系统账号
     */
    @PreAuthorize("@ss.hasPermi('driver:system:add')")
    @Log(title = "驶员系统账号", businessType = BusinessType.INSERT)
    @PostMapping("/addDriverUser")
    public AjaxResult addDriverUser(@RequestBody DriverInfo driverInfo)
    {
        SysUser user = new SysUser();
        //司机的手机就是系统的登录账号
        user.setUserName(driverInfo.getPhone());
        user.setPhonenumber(driverInfo.getPhone());
        user.setNickName(driverInfo.getPhone());
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        //司机默认密码 123456
        user.setPassword(SecurityUtils.encryptPassword("123456"));


        return toAjax(userService.insertDriverUser(user,driverInfo));
    }
}
